package com.xiaoying.server.publish.comomon.ssi;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class GenericIbatisDao<T, PK extends Serializable> extends SqlMapClientDaoSupport {


	// sqlmap.xml定义文件中对应的sqlid   
	public static final String SQLID_INSERT = "insert";   
	public static final String SQLID_UPDATE = "update";   
	public static final String SQLID_UPDATE_PARAM = "updateParam";   
	public static final String SQLID_DELETE_PK = "deletePk";   
	public static final String SQLID_DELETE_PARAM = "deleteParam";   
	public static final String SQLID_TRUNCATE = "truncate";   
	public static final String SQLID_SELECT = "select"; 
	public static final String SQLID_SELECT_ALL = "selectAll";
	public static final String SQLID_SELECT_PK = "selectPk";   
	public static final String SQLID_SELECT_PARAM = "selectParam";   
	public static final String SQLID_SELECT_FK = "selectFk";   
	public static final String SQLID_COUNT = "count";   
	public static final String SQLID_COUNT_PARAM = "countParam";   
	 

	private Class<T> clazz;
	
	@SuppressWarnings("unchecked")
	public GenericIbatisDao() {
		clazz=(Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];	
	}
 
	private String sqlmapNamespace = "";  	
	
	public String getSqlmapNamespace() {
		return sqlmapNamespace;
	}

	public void setSqlmapNamespace(String sqlmapNamespace) {
		this.sqlmapNamespace = sqlmapNamespace;
	}

	/**
	 * deleted by pk
	 * @param pk
	 * @return
	 * @throws SQLException
	 */
	public int delete(PK pk) throws SQLException {
		sqlmapNamespace = clazz.getSimpleName();
		int resultRows = getSqlMapClientTemplate().delete(
				sqlmapNamespace+"."+SQLID_DELETE_PK,pk);
		
		//先缓存然后被批处理
		getSqlMapClientTemplate().getSqlMapClient().startBatch();		
		return resultRows;
	}
	
	/**
	 * findAll
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<T> findAll() {
		sqlmapNamespace = clazz.getSimpleName();
		List<T> list = getSqlMapClientTemplate().queryForList(
				sqlmapNamespace+"."+SQLID_SELECT_ALL);
		return list;
	}

	/**
	 * find by Pk
	 * @param pk
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T findByPk(PK pk) {
		sqlmapNamespace = clazz.getSimpleName();
		T entity = (T) getSqlMapClientTemplate().queryForObject(
				sqlmapNamespace+"."+SQLID_SELECT_PK, pk);
		return entity;
	}

	/**
	 * get Object by pk
	 * @param pk
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T get(PK pk) {
		sqlmapNamespace = clazz.getSimpleName();		
		T entity = (T) getSqlMapClientTemplate().queryForObject(
				sqlmapNamespace+"."+SQLID_SELECT_PK, pk);
		return entity;
	}	
	
	@SuppressWarnings("unchecked")
	public PK insert(T entity) {
		sqlmapNamespace = clazz.getSimpleName();
		return(PK) getSqlMapClientTemplate().insert(
				sqlmapNamespace+"."+SQLID_INSERT, entity);
	}

	public int update(T entity) throws SQLException {
		sqlmapNamespace = clazz.getSimpleName();
		int result = getSqlMapClientTemplate().update(
				sqlmapNamespace+"."+SQLID_UPDATE,entity);
		getSqlMapClientTemplate().getSqlMapClient().startBatch();
		return result;
	}

	public int count() {
		sqlmapNamespace = clazz.getSimpleName();
		int rows = Integer.valueOf(
				getSqlMapClientTemplate().queryForObject(sqlmapNamespace+"."+SQLID_COUNT)+"");
		return rows;
	}

	public List<T> select(SqlParameter param) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<T> selectAll() {
		sqlmapNamespace = clazz.getSimpleName();
		return getSqlMapClientTemplate().queryForList(
				sqlmapNamespace+"."+SQLID_SELECT_ALL);
	}

	@SuppressWarnings("unchecked")
	public List<T> select(Map map) throws SQLException {
		sqlmapNamespace = clazz.getSimpleName();
		return getSqlMapClientTemplate().queryForList(
				sqlmapNamespace+"."+SQLID_SELECT,map);
	}
		
}
